package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.provider.manager.mapper.MerchantEnvironmentMapper;
import com.amigo.online.provider.manager.mapper.MerchantTblMapper;
import com.amigo.online.provider.manager.model.entity.MerchantTbl;
import com.amigo.online.provider.manager.model.entity.User;
import com.amigo.online.provider.manager.mongo.dao.UserDao;
import com.amigo.online.provider.manager.result.MerchantTblInfo;
import com.amigo.online.provider.manager.result.merchant.MerchantCacheInfo;
import com.amigo.online.provider.manager.service.MerchantTblService;
import com.amigo.online.provider.manager.service.UserService;
import com.amigo.online.provider.manager.util.param.dto.RedisKey;

@Service
public class MerchantTblServiceImpl implements MerchantTblService {

    private static Logger logger = LoggerFactory.getLogger(MerchantTblServiceImpl.class);

    @SuppressWarnings("rawtypes")
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserDao userDao;

    @Resource
    private MerchantTblMapper merchantTblMapper;

    @Resource
    private MerchantEnvironmentMapper merchantEnvironmentMapper;

    @Resource
    private UserService userService;

    @Override
    public List<MerchantTblInfo> findMerchantTblList(MerchantTbl merchantTbl) {

        return merchantTblMapper.findMerchantTblList(merchantTbl);
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean checkMerchantTbl(MerchantTbl merchantTbl) {
        merchantTbl.setUpdatedTime(System.currentTimeMillis() / 1000);
        if (merchantTbl.getStatus() != null) {
            redisTemplate.opsForValue().getOperations().delete(RedisKey.MERCHANT_STATUS_KEY + merchantTbl.getUserId());
            redisTemplate.opsForHash().delete(RedisKey.USER_REDIS_KEY, merchantTbl.getUserId());
            if (merchantTbl.getStatus() == 2 && merchantTbl.getUserId() != 100) {
                User user = new User();
                user.setId(merchantTbl.getUserId());
                user.setType(2);
                userService.updateType(user);
            }
            if (merchantTbl.getStatus() == 0 && merchantTbl.getUserId() != 100) {
                User user = new User();
                user.setId(merchantTbl.getUserId());
                user.setType(1);
                userService.updateType(user);
            }
        }
        return merchantTblMapper.updateMerchantTbl(merchantTbl) > 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean deleteMerchantTbl(MerchantTbl merchantTbl) {
        merchantTbl.setDeleted((short) 1);
        merchantTbl.setDeletedTime(System.currentTimeMillis() / 1000);
        if (merchantTblMapper.updateMerchantTbl(merchantTbl) > 0) {
            // 删除商户缓存
            redisTemplate.opsForValue().getOperations().delete(RedisKey.MERCHANT_CACHE_INFO + merchantTbl.getId());

            if (merchantTbl.getUserId() != 100) {
                User user = new User();
                user.setId(merchantTbl.getUserId());
                user.setType(1);
                userService.updateType(user);
                // 删除用户商户状态缓存
                redisTemplate.opsForValue().getOperations()
                        .delete(RedisKey.MERCHANT_STATUS_KEY + merchantTbl.getUserId());
            }
            return true;
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean saveMerchantTbl(MerchantTbl merchantTbl) {

        // 客服添加直接审核成功
        merchantTbl.setStatus(2);
        merchantTbl.setUserId(Long.valueOf(100));
        merchantTbl.setCreatedTime(System.currentTimeMillis() / 1000);
        if (merchantTblMapper.saveMerchantTbl(merchantTbl) > 0) {
            MerchantCacheInfo merchantTblInfo = new MerchantCacheInfo();
            merchantTblInfo.setId(merchantTbl.getId());
            merchantTblInfo.setMerchantName(merchantTbl.getMerchantName());
            merchantTblInfo.setLng(merchantTbl.getLng());
            merchantTblInfo.setLat(merchantTbl.getLat());
            merchantTblInfo.setStatus(2);
            merchantTblInfo.setCategoryId(merchantTbl.getCategoryId());
            // 商家缓存 信息
            redisTemplate.opsForValue().set(RedisKey.MERCHANT_CACHE_INFO + merchantTbl.getId(), merchantTblInfo);
            return true;
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean updateMerchantTbl(MerchantTbl merchantTbl) {
        if (merchantTblMapper.updateMerchantTbl(merchantTbl) > 0) {
            merchantTbl.setUpdatedTime(System.currentTimeMillis() / 1000);
            MerchantCacheInfo merchantTblInfo = new MerchantCacheInfo();
            merchantTblInfo.setId(merchantTbl.getId());
            merchantTblInfo.setMerchantName(merchantTbl.getMerchantName());
            merchantTblInfo.setLng(merchantTbl.getLng());
            merchantTblInfo.setLat(merchantTbl.getLat());
            merchantTblInfo.setStatus(merchantTbl.getStatus());
            merchantTblInfo.setCategoryId(merchantTbl.getCategoryId());
            // 商家缓存 信息
            redisTemplate.opsForValue().set(RedisKey.MERCHANT_CACHE_INFO + merchantTbl.getId(), merchantTblInfo);
            if (!StringUtils.isBlank(merchantTbl.getLng()) || !StringUtils.isBlank(merchantTbl.getLat())) {
                try {
                    MerchantTbl info = new MerchantTbl();
                    info.setId(merchantTbl.getId());
                    MerchantTblInfo infos = merchantTblMapper.findMerchantTblList(info).get(0);
                    Long counts = userDao.updateMerchantLocation(infos);
                    logger.info("更新的个{}", counts);
                } catch (Exception e) {
                    logger.error("mongo更新失败");
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean manager(Long startId, Long endId) {
        List<MerchantTblInfo> findMerchantTblByIdRange = merchantTblMapper.findMerchantTblByIdRange(startId, endId);
        for (MerchantTblInfo info : findMerchantTblByIdRange) {
            Long counts = userDao.updateMerchantLocation(info);
        }
        return true;
    }

}
